import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.util.HashMap;
import java.util.Map;

public class CodeGen {

    //    public static String url = "jdbc:oracle:thin:@127.0.0.1:1521:XE";
//    public static String username = "mp";
//    public static String password = "123456";
    public static String url = "jdbc:mysql://172.24.161.106:3306/dfmiecdms?useUnicode=true&autoReconnect=true&failOverReadOnly=false&characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8";
    //public static String url="jdbc:oracle:thin:@172.17.44.121:8080:siebdb";
    public static String username = "mysqladmin";
    public static String password = "Dfmiec@123";

    public static String dbType = "mysql";
    public static String author = "chenb";
    public static String projectName = "dfmiecdms";
    public static String parentPackage = "basedata";
    public static String moduleName = "product";
    public static String templatePath = "templates/dfmiecdms";
    public static String outputDir = "D:\\dfmiecdms";
    public static String ignoreColumns = "CREATOR, CREATED_NAME, CREATED_DATE, MODIFIER,MODIFY_NAME,LAST_UPDATED_DATE,IS_ENABLE,CREATED_BY,CREATED_TIME,LAST_UPD,LAST_UPD_BY,LAST_UPDATED_BY,LAST_UPDATED_TIME,TENANCY_ID,DELETED";
    public static String tablePrefix = "T_DMS_";

    public static String getEntityName(String tableName) {
        return StringUtils.underlineToCamel(tableName.replace(tablePrefix, ""));
    }

    public static void genCode(String... tableNames) {
        //自定义技输出文件目录
        Map<OutputFile, String> pathInfoMap = new HashMap<>();
        pathInfoMap.put(OutputFile.xml, outputDir + "\\resources\\mybatis\\mapper\\" + dbType + "\\"+"asc"+"\\"+ moduleName);
        pathInfoMap.put(OutputFile.other, outputDir + "\\vue\\" +parentPackage+"\\"+ moduleName);
        //模板属性可参照 AbstractTemplateEngine.getObjectMap

        FastAutoGenerator.create(url, username, password)
                .globalConfig(builder -> {
                    builder.author(author) // 设置作者.
                            .commentDate("yyyy-MM-dd")//注释日期
                            .fileOverride()// 覆盖以已成文件
                            .enableSwagger() // 开启 swagger 模式
                            //.disableOpenDir()
                            .dateType(DateType.ONLY_DATE)
                            .outputDir(outputDir + "\\java\\"); // 指定输出目录
                })
                .packageConfig(builder -> {
                    builder.parent("com.szlanyou.cloud.asc" + (StringUtils.isNotBlank(parentPackage)?"."+parentPackage:"")) // 设置父包名
                            .moduleName(moduleName) // 设置父包模块名
                            .pathInfo(pathInfoMap)
                            .other("%s");
                    // 生成路径
                })
                .strategyConfig(builder -> {
                    for (String tableName : tableNames) {
                        builder.addInclude(tableName); // 设置需要生成的表名
                    }

                    builder.addTablePrefix(tablePrefix) // 设置过滤表前缀
                            .entityBuilder().fileOverride()
                            .enableTableFieldAnnotation()
                            //.formatFileName("%sEntity")
                            //.convertFileName()
                            .logicDeleteColumnName("IS_ENABLE")
                            .logicDeletePropertyName("IS_ENABLE")
                            .addSuperEntityColumns("CREATOR", "CREATED_NAME", "CREATED_DATE", "MODIFIER","MODIFY_NAME","LAST_UPDATED_DATE","IS_ENABLE")
                            //.addIgnoreColumns("created_by", "created_time", "LAST_UPDATED_BY", "LAST_UPDATED_TIME")
                            .idType(IdType.ASSIGN_UUID)
                            .enableLombok()
                            .entityBuilder().superClass("com.szlanyou.cloud.extend.common.entity.BaseEntity")
                            .controllerBuilder().fileOverride().enableRestStyle().enableHyphenStyle().superClass("BaseController")
                            .mapperBuilder().fileOverride().enableBaseColumnList().enableBaseResultMap()
                            .serviceBuilder().fileOverride()
                            .build();
                })
                .templateConfig(builder -> {
                    builder.controller(templatePath + "/Controller.java")
                            .entity(templatePath + "/entity.java")
                            .build();
                })
                .injectionConfig(builder -> {
                    builder.beforeOutputFile((tableInfo, map) -> {
                        String entName = StringUtils.firstToLowerCase(tableInfo.getEntityName());
                        Map<String, String> customFile = new HashMap<>();
                        // DTO
//                        customFile.put(StringUtils.firstToLowerCase(entName) + "Manage.vue", templatePath + "/Manage.vue.ftl");
                        customFile.put("index.vue", templatePath + "/index.vue.ftl");
                        customFile.put("component/addAndEdit.vue", templatePath + "/addAndEdit.vue.ftl");
                        customFile.put(StringUtils.firstToLowerCase(entName) + "Api.js", templatePath + "/api.js.ftl");
                        customFile.put("router.txt", templatePath + "/router.txt.ftl");
                        customFile.put("db.sql", templatePath + "/db.sql.ftl");
                        builder.customFile(customFile).fileOverride();
                        System.out.println(tableInfo);
                        System.out.println(map);
                    });
                    Map<String, Object> customMap = new HashMap<>();
                    customMap.put("ignoreColumns", ignoreColumns);
                    customMap.put("parentPackage", parentPackage);
                    builder.customMap(customMap);
                })

                .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                .execute();
    }


    public static void main(String[] args) {
        author = "huq";
        parentPackage = "";
        moduleName = "";
        tablePrefix="t_dms_";
        genCode("t_dms_se_dlr_part_price_import");
    }
}
